package com.ftg.wymall.admin.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.wymall.admin.dto.UserInfo;
import com.ftg.wymall.admin.entity.UmsAdmin;
import com.ftg.wymall.admin.entity.UmsMenu;
import com.ftg.wymall.admin.mapper.UmsMenuMapper;
import com.ftg.wymall.admin.service.IUmsMenuService;
import com.ftg.wymall.admin.vo.UmsAdminVo;
import com.ftg.wymall.admin.vo.UmsMenuTreeVo;
import com.ftg.wymall.comm.tool.ConventUtil;
import com.ftg.wymall.comm.tool.TokenUtil;
import com.ftg.wymall.comm.tool.UserPasswordCrypto;
import com.ftg.wymall.comm.vo.CodeStatusEnum;
import com.ftg.wymall.comm.vo.RespMessUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2023-10-20
 */
@Service
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements IUmsMenuService {

    @Override
    public RespMessUtil<List<UmsMenuTreeVo>> showMenuByAdminId(String token) {

        JSONObject jsonObject = TokenUtil.parseTokenWithObject(token);
        Long adminId = jsonObject.getLong("id");

        //取数据库里面的数据
        List<UmsMenu> menus = this.getBaseMapper().showMenuByAdminId(adminId);

        //所数据转换成树形结构
        List<UmsMenuTreeVo> tree = ConventUtil.createTree(menus, UmsMenuTreeVo.class);

        return RespMessUtil.build(CodeStatusEnum.SUCCESS,tree);
    }

    @Override
    public RespMessUtil<List<UmsMenuTreeVo>> allMenuForTree() {

        List<UmsMenu> all = this.getBaseMapper().selectList(null);

        if(Objects.nonNull(all)){
            List<UmsMenuTreeVo> tree = ConventUtil.createTree(all, UmsMenuTreeVo.class);
            return RespMessUtil.build(CodeStatusEnum.SUCCESS,tree);
        }
        return RespMessUtil.build(CodeStatusEnum.FAILURE);
    }

}
